<template>
  <div class="home-layout">
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button to="/search" class="search-btn" icon="search" type="primary" slot="title">搜索</van-button>
    </van-nav-bar>
    <van-tabs v-model="active" animated swipeable class="channel-tabs">
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <article-list :channelId="item.id" />
      </van-tab>
      <div slot="nav-right" class="nav-right">
        <i slot="icon" class="link link-gengduo" @click="more"></i>
      </div>
      <div slot="nav-right" class="placeholder"></div>
    </van-tabs>

    <van-popup v-model="isShowEditChannel" closeable position="bottom" close-icon-position="top-left"
      :style="{ height: '90%' }">
      <channel-item :channels='channels' :activeIndex='active' @sendData="getChannelEditData" />
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import { getItem } from '@/utils/storage.js'
import ArticleList from './components/ArticleList.vue'
import ChannelItem from './components/channel-item.vue'
const HTTT_CHANNEL = 'hmtt_channel'
export default {
  data () {
    return {
      active: 0,
      channels: [],
      isShowEditChannel: false
    }
  },
  methods: {
    async loadUserChannels () {
      const data = getItem(HTTT_CHANNEL)
      if (!this.$store.state.user && data) {
        this.channels = data
      } else {
        try {
          const { data: res } = await getUserChannels()
          this.channels = res.data.channels
        } catch (err) {
          this.$toast('获取频道失败')
        }
      }
    },
    getChannelEditData (e) {
      this.isShowEditChannel = e.close
      this.active = e.index
    },
    more () {
      this.isShowEditChannel = true
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {
    ArticleList,
    ChannelItem
  },
  created () {
    this.loadUserChannels()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.home-layout {
  padding-bottom: 100px;
  padding-top: 174px;
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 32px;
    border: none;
  }
  /deep/ .van-icon-search {
    color: #fff;
  }
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      width: 100%;
    }
    .van-tab {
      min-width: 200px;
      height: 82px;
      border-right: 1px solid #edeff3;
      .van-tab__text {
        font-size: 28px;
        color: #777;
      }
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: #3296fa;
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tab--active {
      .van-tab__text {
        font-size: 30px;
        color: #333;
      }
    }
    .van-tabs__nav {
      padding: 0;
      // padding-right: 66px;
    }
    .nav-right {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      background: rgba(255, 255, 255, 0.9);
      display: flex;
      justify-content: center;
      align-items: center;
      .link {
        font-size: 33px;
      }
      &::before {
        content: '';
        position: absolute;
        left: 0;
        width: 2px;
        height: 82px;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: 100% 100%;
      }
    }
    .placeholder {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
  }
}
</style>
